﻿using Anke.SHManage.BLL;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.IM.Controllers
{
    public class I_ApplyOrderController : Controller
    {
        //
        // GET: /IM/I_ApplyList/

        public ActionResult I_ApplyOrderList()
        {
            ViewBag.CheckAll = UserOperateContext.Current.getGongneng(E_IMPermisson.CKALLSL); //71查看所有申领
            ViewBag.userId = UserOperateContext.Current.Session_UsrInfo.ID;
            return View();
        }

        /// <summary>
        /// 获取申领单list
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetApplyList()
        {
            DateTime StartDate = Convert.ToDateTime(Request.Form["StartDate"]);
            DateTime EndDate = Convert.ToDateTime(Request.Form["EndDate"]);
            string userID = Request.Form["SelectUsers"].ToString();
            string ApplyType = Request.Form["ApplyType"].ToString();
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求的页码
            int pageIndex = int.Parse(Request.Form["page"]);
            int total = 0;
            //最大权限 
            E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();        
            //根据用户登录获取人员仓库关系里的所能控制的所有仓库
            List<int> listUserStorage = UserOperateContext.Current.Session_StorageRelated.listUserStorage;

            //全部的权限查看全部的
            if (em == E_StatisticsPermisson.ALL)
            {
                if(userID=="0")
                { userID = ""; }
                string centerID = "";
                var list = new I_ApplyBLL().GetApplyList(pageIndex, pageSize, ref total, StartDate, EndDate, userID, ApplyType, centerID, listUserStorage);
                return Json(new { total = total, rows = list }, "appliction/json", JsonRequestBehavior.AllowGet);

            }
            //分中心权限只查看所属分中心的
            else if (em == E_StatisticsPermisson.CENTER)
            {
                string centerID = "";
                //string centerID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID != null ? UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID : "";
                var list = new I_ApplyBLL().GetApplyOrderList(pageIndex, pageSize, ref total, StartDate, EndDate, userID, ApplyType, centerID, listUserStorage);
                return Json(new { total = total, rows = list }, "appliction/json", JsonRequestBehavior.AllowGet);
            }
            //个人权限只查看本人的
            else
            {
                string centerID = "";
                var list = new I_ApplyBLL().GetApplyOrderList(pageIndex, pageSize, ref total, StartDate, EndDate, userID, ApplyType, centerID, listUserStorage);
                return Json(new { total = total, rows = list }, "appliction/json", JsonRequestBehavior.AllowGet);
            }
        }
        //获取申请详单
        [HttpPost]
        public ActionResult GetApplyDetail()
        {
            string ApplyCode = Request.Form["ApplyID"].ToString();
            var list = new I_ApplyBLL().getApplyDetailList(ApplyCode);
            return Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
        }
    }
}
